package
{
	import controller.EnemyController;
	import controller.MapCameraController;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.geom.Vector3D;
	import flash.net.Responder;
	
	import game.Headquarter;
	
	import gameObject.BattleMap;
	import gameObject.Enemy;
	import gameObject.GameObjectBase;
	
	import ui.LoginWindow;
	
	import views.GameView;
	
	[SWF(width="820",height="550",backgroundColor="0x333333",frameRate="60")]
	public class lastCitadel extends Sprite
	{
		public function lastCitadel()
		{
			Global.stage = stage;
			
			init2D();
			initPhpServer();
		}
		
		private function init2D():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
		}
		
		private function initPhpServer():void
		{
			Global.phpServer.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);  //如果没连上服务器不会报错
			function onNetStatus(e:NetStatusEvent):void{}
			
			Global.phpServer.connect(Global.phpGateway);
			Global.phpServer.call("setup.getfolder", new Responder(gotP2pIP));//从php服务端取回p2p服务的ip地址
		}
		
		private function gotP2pIP(obj:Object):void
		{
			var xml:XML = new XML(obj);
			Global.p2pAddress = xml.p2p;
			
			init3D();
			//initUI();
			test();
			
		}
		
		private function init3D():void
		{
			stage.addChild(new GameView);
		}
		
		private function initUI():void
		{
			stage.addChild(new LoginWindow);
		}
		////////////////////////////////////
		
		private function test():void
		{
			var map:BattleMap = new BattleMap(2,1);
			Global.gameView.scene.addChild(map);
			map.addEventListener(Event.COMPLETE,onMapComplete);
			
			for(var i:uint=0;i<5;i++)
			{
				var mesh:Enemy = new Enemy(1);
				Global.gameView.scene.addChild(mesh);
				mesh.addEventListener(Event.COMPLETE,onMeshComplete);
			}
		}
		
		private function onMapComplete(e:Event):void
		{
			var map:BattleMap = e.currentTarget as BattleMap;
			
			var con:MapCameraController = new MapCameraController;
			map.controller = con;
			con.followObject(new GameObjectBase);
			con.mouseRotateCamera();
		}
		
		private function onMeshComplete(e:Event):void
		{
			var mesh:Enemy = e.currentTarget as Enemy;
			var eneCon:EnemyController = new EnemyController(new Vector3D(0,0,150));
			mesh.controller = eneCon;
			
			eneCon.start();
		}
	}
}